/*
 * @Author: HotSuitor
 * @Date: 2020-03-12 15:09:36
 * @LastEditors: hs
 * @LastEditTime: 2020-03-12 15:47:07
 * @Description: hotsuitor@qq.com
 */
package main

import (
	"fmt"
	"time"
)

func main() {
	go say("World")
	say("Hello")

	s := []int{7,2,8,-9,4,0}
	// chan 通道 <- c 赋值
	c := make(chan int)
	go sum(s[:len(s)/2], c)
	go sum(s[len(s)/2:], c)
	x,y := <-c, <-c
	fmt.Printf("x=%d y=%d x+y=%d\n", x, y, x+y)
}

func say(s string) {
	for i := 0; i < 5; i++ {
		time.Sleep(time.Millisecond*100)
		fmt.Println(s)
	}
}

func sum(s []int, c chan int) {
	sum := 0
	for _, v := range s {
		sum += v
	}
	c <- sum // 把sum发送到管道c
}
